package com.ifp.adapter.process;

import java.util.*;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import com.ifp.adapter.exception.FormatException;
import com.ifp.adapter.exception.UnformatException;
import com.ifp.core.exception.BaseException;
import com.ifp.core.log.Trace;
import com.ifp.core.util.StringUtil;

/**
 * 项目名：ifp-gateway <br>
 * 包名：com.ifp.opengate.boot.utils <br>
 * 类名：JSONProcessor <br>
 * 描述： 重写 JSONProcessor 取消打印日志
 * 
 * @author liulx
 * @create 2022-07-05 16:52
 */
@Component("JSONProcessor")
@Lazy(true)
@Primary
public class JSONProcessorNoLog extends BaseProcessor {

    @Override
    public Map<String, Object> unformat(String message) throws BaseException {
        // Trace.log("ADAPTER", 1, "JSONProcessorNoLog receive msg is :{}", new Object[] {message});
        Map<String, Object> jsonMap;
        try {
            JSONObject jsonObject = new JSONObject(message);
            jsonMap = this.jsonToMap(jsonObject);
        } catch (Exception var4) {
            throw new UnformatException("SAFU0001", "报文解析异常", var4);
        }
        return jsonMap;
    }

    public String format(Map<String, Object> dataMap) throws BaseException {
        try {
            String repMsg = this.mapToJson(dataMap);
            Trace.log("ADAPTER", 0, "json format Context is :{}", new Object[] {repMsg});
            return repMsg;
        } catch (Exception var4) {
            throw new FormatException("SAFF0001", "报文组装异常", var4);
        }
    }

    public String mapToJson(Map<String, Object> dataMap) {
        if (null == dataMap) {
            return "{}";
        } else {
            StringBuffer repMsg = new StringBuffer();
            String key = null;
            Object value = null;
            repMsg.append("{");

            for (Iterator var5 = dataMap.entrySet().iterator(); var5.hasNext(); repMsg.append(",")) {
                Map.Entry<String, Object> entry = (Map.Entry)var5.next();
                key = (String)entry.getKey();
                repMsg.append("\"").append(key).append("\":");
                value = dataMap.get(key);
                if (null != value && !(value instanceof String)) {
                    if (value instanceof Map) {
                        repMsg.append(this.mapToJson((Map)value));
                    } else if (value instanceof List) {
                        repMsg.append(this.listToJson((List)value));
                    } else {
                        switch (this.getOutHtmlFilter()) {
                            case 1:
                                repMsg.append("\"")
                                    .append(StringUtil.formatJSONText(StringUtil.formatHtmlText(value.toString())))
                                    .append("\"");
                                break;
                            case 2:
                                repMsg.append("\"")
                                    .append(StringUtil.formatJSONText(StringUtil.formatHtmlText1(value.toString())))
                                    .append("\"");
                                break;
                            case 3:
                                repMsg.append("\"")
                                    .append(StringUtil.formatJSONText(StringUtil.formatHtmlText2(value.toString())))
                                    .append("\"");
                                break;
                            default:
                                repMsg.append("\"").append(StringUtil.formatJSONText(value.toString())).append("\"");
                        }
                    }
                } else {
                    switch (this.getOutHtmlFilter()) {
                        case 1:
                            repMsg.append("\"")
                                .append(StringUtil.formatJSONText(StringUtil.formatHtmlText((String)value)))
                                .append("\"");
                            break;
                        case 2:
                            repMsg.append("\"")
                                .append(StringUtil.formatJSONText(StringUtil.formatHtmlText1((String)value)))
                                .append("\"");
                            break;
                        case 3:
                            repMsg.append("\"")
                                .append(StringUtil.formatJSONText(StringUtil.formatHtmlText2((String)value)))
                                .append("\"");
                            break;
                        default:
                            repMsg.append("\"").append(StringUtil.formatJSONText((String)value)).append("\"");
                    }
                }
            }

            if (repMsg.toString().lastIndexOf(",") != -1) {
                repMsg.deleteCharAt(repMsg.length() - 1);
            }

            repMsg.append("}");
            return repMsg.toString();
        }
    }

    public String listToJson(List<Object> dataList) {
        if (dataList == null) {
            return "[]";
        } else {
            StringBuffer repMsg = new StringBuffer();
            repMsg.append("[");

            for (int i = 0; i < dataList.size(); ++i) {
                if (dataList.get(i) instanceof Map) {
                    repMsg.append(this.mapToJson((Map)dataList.get(i)));
                }

                if (i < dataList.size() - 1) {
                    repMsg.append(",");
                }
            }

            repMsg.append("]");
            return repMsg.toString();
        }
    }

    public List<Object> jsonToList(JSONArray jsonArray) throws Exception {
        List<Object> jsonList = new ArrayList();

        try {
            for (int i = 0; i < jsonArray.length(); ++i) {
                Object valueObject = jsonArray.get(i);
                if (valueObject instanceof JSONArray) {
                    jsonList.add(this.jsonToList((JSONArray)valueObject));
                } else if (valueObject instanceof JSONObject) {
                    jsonList.add(this.jsonToMap((JSONObject)valueObject));
                } else {
                    jsonList.add(StringUtil.unformatHtmlText(String.valueOf(valueObject)));
                }
            }

            return jsonList;
        } catch (Exception var5) {
            Trace.log("ADAPTER", 3, "JSONArray To List Error：{}", new Object[] {jsonArray.toString()});
            throw var5;
        }
    }

    public Map<String, Object> jsonToMap(JSONObject jsonObject) throws Exception {
        Map<String, Object> jsonMap = new LinkedHashMap();
        Set<String> keySet = jsonObject.keySet();

        try {
            Iterator var4 = keySet.iterator();

            while (var4.hasNext()) {
                String key = (String)var4.next();
                Object valueObject = jsonObject.get(key);
                if (valueObject instanceof JSONArray) {
                    jsonMap.put(key, this.jsonToList((JSONArray)valueObject));
                } else if (valueObject instanceof JSONObject) {
                    jsonMap.put(key, this.jsonToMap((JSONObject)valueObject));
                } else {
                    jsonMap.put(key, StringUtil.unformatHtmlText(String.valueOf(valueObject)));
                }
            }

            return jsonMap;
        } catch (Exception var7) {
            Trace.log("ADAPTER", 3, "jsonObject To Map Error：{}", new Object[] {jsonObject.toString()});
            throw var7;
        }
    }

    @Autowired
    public void setType(@Value("J") String type) {
        this.type = type;
    }

}
